package edu.whu.irlab.woscrawl;

import edu.whu.irlab.woscrawl.selenium.Worker;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by zhongwei on 16/5/20.
 */
public class Entry {

    private static int start;
    private static int end;
    private static String firefoxPath;
    private static String savePath;

    public static void main(String[] args) {
        if (args.length != 4) {
            System.out.println("usage: java -jar wos_crawl.jar start end \"firefoxPath\" \"savePath\"");
            System.exit(0);
        }
        start = Integer.parseInt(args[0]);
        end = Integer.parseInt(args[1]);
        firefoxPath = args[2];
        savePath = args[3];
        if (!firefoxPath.contains("\"") || !savePath.contains("\"")) {
            System.out.println("usage: java -jar wos_crawl.jar start end \"firefoxPath\" \"savePath\"");
            System.exit(0);
        }
        firefoxPath = firefoxPath.replaceAll("\"", "");
        savePath = savePath.replaceAll("\"", "");
        Set<Integer> set = new HashSet<Integer>();
        File file = new File(savePath);
        if (!file.exists())
            file.mkdir();
        File[] arr = file.listFiles();
        if (arr != null) {
            for (File f : arr) {
                if (f.getName().equals(".DS_Store")) continue;
                String valS = f.getName().replace(".txt", "");
                int val = Integer.parseInt(valS);
                set.add(val);
            }
        }
        int load = (end - start + 1) / 5;
        int left = end - load * 5;
        for (int i = 0; i < 5; i++) {
            new Worker(start + i * load, start + (i + 1) * load - 1, firefoxPath, savePath, "thread" + i, set).start();
        }
        if (left > 0)
            new Worker(start + 5 * load, end, firefoxPath, savePath, "thread6", set).start();
    }

}
